#include "CAnimationEngine.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "CPlayer.h"
#include <fstream>

CAnimationEngine::CAnimationEngine(void)
{
	m_nNumAni = 0;
	m_pAniID = NULL;
}

CAnimationEngine::~CAnimationEngine(void)
{
	for(unsigned int i = 0; i < m_vAnimations.size(); ++i)
		delete m_vAnimations[i];
	//for(unsigned int i = 0; i < m_vPlayingAnimations.size(); ++i)
	//	delete m_vPlayingAnimations[i];
}

void CAnimationEngine::SetAniInfo(int AniID, int xpos, int ypos)
{
	if((int)m_vAnimations.size() > AniID)
	{
		m_vAnimations[AniID]->SetPosX(xpos);
		m_vAnimations[AniID]->SetPosY(ypos);
	}
}

void CAnimationEngine::Render()
{

	//for(unsigned int i = 0; i < m_vPlayingAnimations.size(); ++i)
	//{
	//	m_vPlayingAnimations[i]->Render();
	//}
	for(unsigned int i = 0; i < m_vAnimations.size(); ++i)
	{
		if(m_vAnimations[i]->GetPlaying())
			m_vAnimations[i]->Render();
	}
}

void CAnimationEngine::Update(float dt)
{
	//vector<CAnimation*> temp;
	//for(unsigned int i = 0; i < m_vPlayingAnimations.size(); ++i)
	//{
	//	bool docontinue = false;
	//	if(m_vRemoveAnimation.size())
	//	{
	//		vector<int*> tempint;
	//		for(unsigned int k = 0; k < m_vRemoveAnimation.size(); k++)
	//		{
	//			if(*m_vRemoveAnimation[k] != i)
	//				tempint.push_back(m_vRemoveAnimation[k]);
	//			else
	//			{
	//				delete m_vPlayingAnimations[i];
	//				m_vRemoveAnimation.clear();
	//				m_vRemoveAnimation = tempint;
	//				docontinue = true;
	//				break;
	//			}
	//		}
	//		m_vRemoveAnimation.clear();
	//		m_vRemoveAnimation = tempint;
	//	}

	//	if(docontinue)
	//		continue;
	//	m_vPlayingAnimations[i]->Update(dt);
	//	if(m_vPlayingAnimations[i]->GetPlaying() == true)
	//		temp.push_back(m_vPlayingAnimations[i]);
	//	else
	//		delete m_vPlayingAnimations[i];
	//}

	for( unsigned int i = 0 ; i < m_vAnimations.size() ; ++i )
	{
		if( m_vAnimations[ i ]->GetPlaying() )
		{
			m_vAnimations[ i ]->Update( dt );
		}
	}
	//m_vPlayingAnimations.clear();
	//m_vPlayingAnimations = temp;

	//for(unsigned int i = 0; i < m_vPlayingAnimations.size(); ++i)
	//	m_vPlayingAnimations[i]->SetAniID(i);
}

CAnimation* CAnimationEngine::GetAnimations(unsigned int i ) 
{ 
	if(i <= m_vAnimations.size())
		return m_vAnimations[i];
	else
		return NULL;
}

//CAnimation* CAnimationEngine::GetPlayingAnimations(unsigned int i ) 
//{
//	if(i < m_vPlayingAnimations.size())
//		return m_vPlayingAnimations[i];
//	else
//		return NULL;
//}

CAnimationEngine* CAnimationEngine::GetInstance()
{
	static CAnimationEngine instance;
	return &instance;
}

//int* CAnimationEngine::AddPlayingAnimations(int AniID,  bool bFlipped)
//{
//	int* temp = NULL;
//	if((unsigned int)AniID < m_vAnimations.size())
//	{
//		CAnimation* ani = new CAnimation(*m_vAnimations[AniID]);
//		ani->SetPlaying(true);
//		temp = new int;
//		*temp = m_vPlayingAnimations.size();
//		ani->SetAniID(temp);
//
//		if(!bFlipped)
//			ani->SetIndexID(AniID);
//		else
//			ani->SetIndexID(-AniID);
//
//		ani->SetFlipped(bFlipped);
//		m_vPlayingAnimations.push_back(ani);
//	}
//	return temp;
//}

//void CAnimationEngine::RemovePlayingAnimations(int* AniID)
//{
//	m_vRemoveAnimation.push_back(AniID);
//}

//void CAnimationEngine::RemoveAll()
//{
//	for(unsigned int i = 0; i < m_vPlayingAnimations.size(); ++i)
//		delete m_vPlayingAnimations[i];
//	m_vPlayingAnimations.clear();
//}

int CAnimationEngine::Load(char* filename, int red, int green, int blue)
{
	ifstream read;
	read.open(filename, ios::in | ios::binary);
	read.beg;
	CAnimation* animation = new CAnimation();

	if(read.is_open())
	{
		int numframes = 0;

		int aninamelength = 0;
		int trinamelength = 0;
		int pathlength = 0;

		read.read((char*)&numframes, sizeof(int));
		animation->SetNumFrames(numframes);
		for(int i = 0; i < numframes; ++i)
		{
			CFrames* frame = new CFrames();

			bool hasframe = false;
			bool hascollision = false;
			bool hasweapon = false;
			bool hasanchor = false;
			int anchorx = 0;
			int anchory = 0;
			int frameduration = 0;
			RECT framerect = {0,0,0,0};
			RECT collisionrect = {0,0,0,0};
			RECT weaponrect = {0,0,0,0};

			read.read((char*)&hasframe, sizeof(bool));
			frame->SetHasFrameRect(hasframe);
			if(hasframe)
			{
				read.read((char*)&framerect.left, sizeof(int));
				read.read((char*)&framerect.top, sizeof(int));
				read.read((char*)&framerect.bottom, sizeof(int));
				read.read((char*)&framerect.right, sizeof(int));
				frame->SetFrameX(framerect.left);
				frame->SetFrameY(framerect.top);
				frame->SetFrameRect(framerect);
			}

			read.read((char*)&hasweapon, sizeof(bool));
			frame->SetHasWeaponRect(hasweapon);
			if(hasweapon)
			{
				read.read((char*)&weaponrect.left, sizeof(int));
				read.read((char*)&weaponrect.top, sizeof(int));
				read.read((char*)&weaponrect.bottom, sizeof(int));
				read.read((char*)&weaponrect.right, sizeof(int));
				frame->SetWeaponX(weaponrect.left);
				frame->SetWeaponY(weaponrect.top);
				frame->SetWeaponRect(weaponrect);
			}

			read.read((char*)&hascollision, sizeof(bool));
			frame->SetHasCollisionRect(hascollision);
			if(hascollision)
			{
				read.read((char*)&collisionrect.left, sizeof(int));
				read.read((char*)&collisionrect.top, sizeof(int));
				read.read((char*)&collisionrect.bottom, sizeof(int));
				read.read((char*)&collisionrect.right, sizeof(int));
				frame->SetCollisionX(collisionrect.left);
				frame->SetCollisionY(collisionrect.top);
				frame->SetCollisionRect(collisionrect);
			}
			read.read((char*)&hasanchor, sizeof(bool));
			frame->SetHasCollisionRect(hasanchor);
			if(hasanchor)
			{
				read.read((char*)&anchorx, sizeof(int));
				read.read((char*)&anchory, sizeof(int));
				anchorx -= framerect.left;
				anchory -= framerect.top;
				frame->SetAnchorX(anchorx);
				frame->SetAnchorY(anchory);
			}
			if(hasframe && hasanchor)
			{
				read.read((char*)&frameduration, sizeof(int));
				frame->SetFrameDuration(frameduration/100.0f);
			}
			animation->AddFrame(frame);
		}
		read.read((char*)&aninamelength, sizeof(int));
		char* animationname = new char[20];
		read.read(animationname, aninamelength+1);
		animationname[aninamelength+1] = 0;
		for(int i = 0; i < 20; i++)
		{
			animationname[i] = animationname[i+1];
		}
		
		animation->SetAnimationName(animationname);

		read.read((char*)&trinamelength, sizeof(int));
		char* triggername = new char[20];
		read.read(triggername, trinamelength+1);
		triggername[trinamelength+1] = 0;
		for(int i = 0; i < 20; i++)
		{
			triggername[i] = triggername[i+1];
		}
		animation->SetTrigger(triggername);

		read.read((char*)&pathlength, sizeof(int));
		char pathname[50];
		read.read(pathname, pathlength+1);
		pathname[pathlength+1] = 0;
		pathname[0] = '/';
		pathname[1] = '/';

		char finaltemp[50] = "Resources/Graphics";

		strcat_s(finaltemp, 50, pathname);
		
		animation->SetID(CSGD_TextureManager::GetInstance()->LoadTexture(finaltemp, D3DCOLOR_XRGB(red, green, blue)));
		animation->SetAniID(m_pAniID);
		animation->SetPlaying(false);

		m_vAnimations.push_back(animation);
		read.close();
	}
	++m_nNumAni;
	return m_vAnimations.size()-1;
}